package com.txunda.zhuohired.adapter.user.home;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.aty.user.home.AllJobListAty;
import com.txunda.zhuohired.aty.user.home.DirectActionAty;
import com.txunda.zhuohired.aty.user.home.PartTimeListAty;
import com.txunda.zhuohired.aty.user.home.TodayNewJobAty;
import com.txunda.zhuohired.bean.user.home.HomeBean;
import com.txunda.zhuohired.util.GlideRoundTransform;

import java.util.List;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2017/12/11 9:49
 * 功能描述：分类adapter
 * 联系方式：jiahuanchen@126.com
 */

public class HomeClassifyAdapter extends RecyclerView.Adapter<HomeClassifyAdapter.ViewHolder> {

    /**
     * 页数下标,从0开始(通俗讲第几页)
     */
    private int mIndex;

    /**
     * 每页显示最大条目个数
     */
    private int mPageSize;

    private Context mContext;

    private final LayoutInflater mLayoutInflater;
    private List<HomeBean.DataBean.ModulesBean> iconList;
    private List<String> dataName;


    public HomeClassifyAdapter(Context context, List<HomeBean.DataBean.ModulesBean> iconList, int
            index, int pageSize) {
        this.mContext = context;
        this.iconList = iconList;
        mPageSize = pageSize;
        mIndex = index;
        mLayoutInflater = LayoutInflater.from(context);

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mLayoutInflater.inflate(R.layout.item_function_home, null));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        /**
         * 在给View绑定显示的数据时，计算正确的position = position + mIndex * mPageSize，
         */
        final int pos = position + mIndex * mPageSize;
        holder.tv_item_function_title.setText(iconList.get(pos).getModules_name());
        //        holder.iv_item_function_icon.setImageResource(iconList.get(pos).getImage());
        Glide.with(mContext).load(iconList.get(pos).getModules_pic()).diskCacheStrategy(DiskCacheStrategy.ALL).error
                (R.mipmap.icon_defult).placeholder(R.mipmap.icon_defult).transform(new GlideRoundTransform(mContext,
                15))
                .dontAnimate().centerCrop().into(holder.iv_item_function_icon);
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ("1".equals(iconList.get(pos).getId())) {
                    Intent intent = new Intent(mContext, TodayNewJobAty.class);
                    intent.putExtra("icon", "1");
                    intent.putExtra("tittle_name", iconList.get(pos).getModules_name());
                    mContext.startActivity(intent);
                } else if ("2".equals(iconList.get(pos).getId())) {
                    Intent intent = new Intent(mContext, DirectActionAty.class);
                    intent.putExtra("icon", "2");
                    intent.putExtra("tittle_name", iconList.get(pos).getModules_name());
                    mContext.startActivity(intent);
                } else if ("3".equals(iconList.get(pos).getId())) {
                    Intent intent = new Intent(mContext, TodayNewJobAty.class);
                    intent.putExtra("icon", "3");
                    intent.putExtra("tittle_name", iconList.get(pos).getModules_name());
                    mContext.startActivity(intent);
                } else if ("4".equals(iconList.get(pos).getId())) {
                    Intent intent = new Intent(mContext, PartTimeListAty.class);
                    intent.putExtra("icon", "4");
                    intent.putExtra("tittle_name", iconList.get(pos).getModules_name());
                    mContext.startActivity(intent);
                } else if ("5".equals(iconList.get(pos).getId())) {
                    Intent intent = new Intent(mContext, AllJobListAty.class);
                    intent.putExtra("icon", "5");
                    mContext.startActivity(intent);
                }
            }
        });

        //        if (pos == 4) {
        //            holder.view.setOnClickListener(new View.OnClickListener() {
        //                @Override
        //                public void onClick(View view) {
        //
        //                }
        //            });
        //        } else if (pos == 3) {
        //            holder.view.setOnClickListener(new View.OnClickListener() {
        //                @Override
        //                public void onClick(View view) {
        //
        //                }
        //            });
        //        } else {
        //            holder.view.setOnClickListener(new View.OnClickListener() {
        //                @Override
        //                public void onClick(View view) {
        //
        //                }
        //            });
        //        }
    }

    @Override
    public int getItemCount() {
        return iconList.size() > (mIndex + 1) * mPageSize ? mPageSize : (iconList.size() - mIndex * mPageSize);
    }

    @Override
    public long getItemId(int position) {
        return position + mIndex * mPageSize;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private View view;
        private TextView tv_item_function_title;
        private ImageView iv_item_function_icon;

        public ViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            iv_item_function_icon = (ImageView) itemView.findViewById(R.id.iv_item_function_icon);
            tv_item_function_title = (TextView) itemView.findViewById(R.id.tv_item_function_title);
        }
    }

}
